<?php


namespace app\mobile\controller;


use think\Db;
use think\facade\Request;

class Home extends LoggingBase
{

    public function Index()
    {
        //今日通話
        $Type = Request::has('Type') ? Request::param('Type') : 1;  //1：今日，2：七天內，3：本月，本季度

        $Where = '';
        if ($Type == 1) {
            //統計今日
            $Date = date('Y-m-d');
            $Where .= " AND DATE_FORMAT(TimeCreated,'%Y-%m-%d')='$Date' ";
        } else if ($Type == 2) {
            //统计七天内
            $Date = date('Y-m-d', strtotime('-7 days')); //保留年-月-日
            $Where .= " AND DATE_FORMAT(TimeCreated,'%Y-%m-%d')>='$Date' ";
        } else if ($Type == 3) {
            $Date = date('Y-m-01');
            $Where .= " AND DATE_FORMAT(TimeCreated,'%Y-%m-%d')>='$Date' ";
        }

        $UserID = $this->m_User->ID;

        //通话分钟
        $TalkTime = round(Db::table('user_call_records')->where("IsDeleted='0' AND UserID='$UserID' $Where")->sum('TalkTime') / 60, 2);
        //拨打电话
        $CallCount = Db::table('user_call_records')->where("IsDeleted='0' AND UserID='$UserID' $Where")->count();
        //接通率
        if ($CallCount == 0) {
            $CallRate = 0;
        } else {
            $CallRate = round(Db::table('user_call_records')->where("IsDeleted='0' AND UserID='$UserID' AND TalkTime>0 $Where")->count() / $CallCount, 4) * 100;
        }

        //待拨打
        $WaitCall = Db::table('user_calls')->where("IsDeleted='0' AND UserID='$UserID' AND IsCalled=0 $Where")->count();
        //待办事项
        $Schedule = 0;

        mb_OutMessage(ErrCode_Normal, '获取成功', array(
            'TalkTime' => $TalkTime,
            'CallCount' => $CallCount,
            'CallRate' => $CallRate,
            'WaitCall' => $WaitCall,
            'Schedule' => $Schedule));
    }

    /**
     * 員工列表
     */
    public function mb_StaffList()
    {
        $Page = Request::has('Page') ? Request::param('Page') : 1;
        $Limit = Request::has('Limit') ? Request::param('Limit') : 10;
        $Keyword = Request::has('Keyword') ? Request::param('Keyword') : '';

        $Where = '';
        if($Keyword!=''){
            $Where .= " AND (Nick LIKE '%$Keyword%' OR WeChat LIKE '%$Keyword%' OR QQ LIKE '%$Keyword%' OR Phone LIKE '%$Keyword%' OR Email LIKE '%$Keyword%') ";
        }

        $CompanyID = $this->m_User->CompanyID;
        $Count = Db::table('user')->where("IsDeleted='0' AND CompanyID='$CompanyID' $Where ")->count();
        $Data = Db::table('user')->field('ID,Nick,WeChat,QQ,Phone,Email')->where("IsDeleted='0' AND CompanyID='$CompanyID' $Where ")->page($Page)->limit($Limit)->select();
        mb_OutMessage(ErrCode_Normal, '', $Data, $Count);
    }
}